package pers.qianyu.diytomcat.http;

import cn.hutool.core.date.*;

import javax.servlet.http.*;
import java.io.*;
import java.nio.charset.*;
import java.text.*;
import java.util.*;

/**
 * @author mizzle rain
 * @date 2021-01-07 10:21
 */
public class Response extends BaseResponse {
    private StringWriter stringWriter;
    private PrintWriter writer;
    private String contentType;
    // 二进制
    private byte[] body;

    private int status;
    private List<Cookie> cookies;
    private String redirectPath;

    public Response() {
        contentType = "text/html";
        stringWriter = new StringWriter();
        writer = new PrintWriter(stringWriter);
        cookies = new ArrayList<>();
    }

    public byte[] getBody() {
        if (Objects.isNull(body)) {
            String content = stringWriter.toString();
            return content.getBytes(StandardCharsets.UTF_8);
        }
        return body;
    }

    public String getCookiesHeader() {
        if (Objects.isNull(cookies)) {
            return "";
        }
        String pattern = "EEE, d MMM yyyy HH:mm:ss 'GMT'";
        SimpleDateFormat formatter = new SimpleDateFormat(pattern, Locale.ENGLISH);
        StringBuffer buffer = new StringBuffer();
        for (Cookie cookie : cookies) {
            buffer.append(String.format("\nSet-Cookie:%s=%s;", cookie.getName(), cookie.getValue()));
            if (cookie.getMaxAge() != -1) {
                buffer.append(String.format("Expires=%s;",
                        formatter.format(DateUtil.offset(new Date(), DateField.MINUTE, cookie.getMaxAge()))));
            }
            if (Objects.nonNull(cookie.getPath())) {
                buffer.append(String.format("Path=%s;", cookie.getPath()));
            }
        }
        return buffer.toString();
    }

    @Override
    public void addCookie(Cookie cookie) {
        cookies.add(cookie);
    }

    @Override
    public void sendRedirect(String s) throws IOException {
        this.redirectPath = s;
    }

    @Override
    public void resetBuffer() {
        stringWriter.getBuffer().setLength(0);
    }

    /******************* automatic generated *************************/
    public List<Cookie> getCookies() {
        return cookies;
    }

    public void setRedirectPath(String redirectPath) {
        this.redirectPath = redirectPath;
    }

    public String getRedirectPath() {
        return redirectPath;
    }

    @Override
    public PrintWriter getWriter() {
        return writer;
    }

    public void setWriter(PrintWriter writer) {
        this.writer = writer;
    }

    @Override
    public int getStatus() {
        return status;
    }

    @Override
    public void setStatus(int status) {
        this.status = status;
    }

    @Override
    public String getContentType() {
        return contentType;
    }

    @Override
    public void setContentType(String contentType) {
        this.contentType = contentType;
    }

    public void setBody(byte[] body) {
        this.body = body;
    }
}
